package com.lab.jumper;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;


import com.lab.logtrack.AppLog;

import java.lang.reflect.Method;

/**
 * Created by ak
 * 组件跳转器,用于Activity、Service的跳转和结束
 */
public class Jumper {

    /**
     * 启动Activity
     *
     * @param context
     * @param intent
     */
    public static void launchActivity(Context context, Intent intent) {
        launchActivity(context, intent, 0, 0);
    }

    /**
     * 启动Activity,可以选择启动动画
     *
     * @param context
     * @param intent
     * @param enterAnim 0表示无动画
     * @param exitAnim  0表示无动画
     */
    public static void launchActivity(Context context, Intent intent, int enterAnim, int exitAnim) {
        if (null != context && null != intent) {
            // 启动Activity
            try {
                // 包装原始Bundle
                BundleCenter.getInstance().wrapIntent(intent, context.getClass().getSimpleName());
                if (context instanceof Application)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception e) {
                // 释放原始Bundle
                BundleCenter.getInstance().removeBundle(intent);
                AppLog.w(e.getMessage());
            }
            // 设置动画
            try {
                if (enterAnim > 0 || exitAnim > 0) {
                    if (context instanceof Activity) {
                        Method method = Activity.class.getMethod("overridePendingTransition", int.class, int.class);
                        method.invoke(context, enterAnim, exitAnim);
                    }
                }
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    /**
     * 启动Activity(需要处理返回结果)
     *
     * @param activity
     * @param intent
     * @param requestCode
     */
    public static void launchActivityForResult(Activity activity, Intent intent, int requestCode) {
        launchActivityForResult(activity, intent, requestCode, 0, 0);
    }

    /**
     * 启动Activity(需要处理返回结果),可以选择启动动画
     *
     * @param activity
     * @param intent
     * @param requestCode
     * @param enterAnim   0表示无动画
     * @param exitAnim    0表示无动画
     */
    public static void launchActivityForResult(Activity activity, Intent intent, int requestCode, int enterAnim, int exitAnim) {
        if (null != activity && null != intent) {
            // 启动Activity
            try {
                // 包装原始Bundle
                BundleCenter.getInstance().wrapIntent(intent, activity.getClass().getSimpleName());
                activity.startActivityForResult(intent, requestCode);
            } catch (Exception e) {
                // 释放原始Bundle
                BundleCenter.getInstance().removeBundle(intent);
                AppLog.w(e.getMessage());
            }
            // 设置动画
            try {
                if (enterAnim > 0 || exitAnim > 0) {
                    Method method = Activity.class.getMethod("overridePendingTransition", int.class, int.class);
                    method.invoke(activity, enterAnim, exitAnim);
                }
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    /**
     * 绑定Service
     *
     * @param context
     * @param intent
     * @param serviceConnection
     * @param flags
     */
    public static void bindService(Context context, Intent intent, ServiceConnection serviceConnection, int flags) {
        if (null != context && null != intent && null != serviceConnection) {
            try {
                context.bindService(intent, serviceConnection, flags);
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    /**
     * 解绑Service
     *
     * @param context
     * @param serviceConnection
     */
    public static void unbindService(Context context, ServiceConnection serviceConnection) {
        if (null != context && null != serviceConnection) {
            try {
                context.unbindService(serviceConnection);
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    /**
     * 启动Service
     *
     * @param context
     * @param intent
     */
    public static void startService(Context context, Intent intent) {
        if (null != context && null != intent) {
            try {
                context.startService(intent);
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    /**
     * 停止Service
     *
     * @param context
     * @param intent
     */
    public static void stopService(Context context, Intent intent) {
        if (null != context && null != intent) {
            try {
                context.stopService(intent);
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

    /**
     * 结束Activity
     *
     * @param activity
     */
    public static void finishActivity(Activity activity) {
        finishActivity(activity, 0, 0);
    }

    /**
     * 结束Activity,可以选择结束动画
     *
     * @param activity
     * @param enterAnim 0表示无动画
     * @param exitAnim  0表示无动画
     */
    public static void finishActivity(Activity activity, int enterAnim, int exitAnim) {
        if (null != activity) {
            // 结束Activity
            try {
                activity.finish();
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
            // 设置动画
            try {
                if (enterAnim > 0 || exitAnim > 0) {
                    Method method = Activity.class.getMethod("overridePendingTransition", int.class, int.class);
                    method.invoke(activity, enterAnim, exitAnim);
                }
            } catch (Exception e) {
                AppLog.w(e.getMessage());
            }
        }
    }

}
